import { createRouter, createWebHistory } from 'vue-router'
import HomeView from '../views/HomeView.vue'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'home',
      component: HomeView,
      meta: {
        requiresAuth: true,
      },
    },
    {
      path: '/login',
      name: 'login',
      component: () => import('../views/LoginView.vue'),
      meta: {
        requiresAuth: false,
      },
    },
    {
      path: '/gly',
      name: 'gly',
      component: () => import('../views/GlyView.vue'),
      meta: {
        requiresAuth: true,
      },
    },
    {
      path: '/gg',
      name: 'gg',
      component: () => import('../views/GgView.vue'),
      meta: {
        requiresAuth: true,
      },
    },
    {
      path: '/fl',
      name: 'fl',
      component: () => import('../views/FlView.vue'),
      meta: {
        requiresAuth: true,
      },
    },
    {
      path: '/cater',
      name: 'cater',
      component: () => import('../views/CaterView.vue'),
      meta: {
        requiresAuth: true,
      },
    },
    {
      path: '/department',
      name: 'department',
      component: () => import('../views/DepartmentView.vue'),
      meta: {
        requiresAuth: true,
      },
    },
    {
      path: '/order',
      name: 'order',
      component: () => import('../views/OrderView.vue'),
      meta: {
        requiresAuth: true,
      },
    },
    {
      path: '/role',
      name: 'role',
      component: () => import('../views/RoleView.vue'),
      meta: {
        requiresAuth: true,
      },
    },
    {
      path: '/menu',
      name: 'menu',
      component: () => import('../views/MenuView.vue'),
      meta: {
        requiresAuth: true,
      },
    },
    {
      path: '/lt',
      name: 'lt',
      component: () => import('../views/LtView.vue'),
      meta: {
        requiresAuth: true,
      },
    },
    {
      path: '/lf',
      name: 'lf',
      component: () => import('../views/LfView.vue'),
      meta: {
        requiresAuth: true,
      },
    },
  ],
})

router.beforeEach((to, from, next) => {
  // 获取token
  const token = localStorage.getItem('loginToken')
  // 判断是否登录
  if (to.path !== '/login') {
    if (!token) {
      next('/login')
    } else {
      next()
    }
  } else {
    next()
  }
})
export default router
